/*
 * @formatter:off
 * Copyright 2022 Cacao
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this common except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * @formatter:on
 */
package springfox.documentation.oas.mappers;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Encoding;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import io.swagger.v3.oas.models.tags.Tag;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import springfox.documentation.schema.ModelSpecification;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Documentation;
import springfox.documentation.service.ModelNamesRegistry;
import springfox.documentation.service.ParameterSpecification;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.Representation;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.service.ResourceListing;
import springfox.documentation.service.Response;
import springfox.documentation.service.SimpleParameterSpecification;
import springfox.documentation.service.VendorExtension;

/**
 * @author XuQx
 * @date 2022/08/26
 */
@Component
public class ServiceModelToOpenApiMapperImpl extends ServiceModelToOpenApiMapper {

    @Autowired
    private VendorExtensionsMapper vendorExtensionsMapper;
    @Autowired
    private LicenseMapper licenseMapper;
    @Autowired
    private ExamplesMapper examplesMapper;
    @Autowired
    private SecurityMapper securityMapper;
    @Autowired
    private SchemaMapper schemaMapper;
    @Autowired
    private StyleEnumMapper styleEnumMapper;
    @Autowired
    private SecuritySchemeMapper securitySchemeMapper;
    
    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    public OpenAPI mapDocumentation(Documentation from) {
        if (from == null) {
            return null;
        }
        OpenAPI openAPI = new OpenAPI();
        if (from.getResourceListing() != null) {
            if (openAPI.getComponents() == null) {
                openAPI.components(new Components());
            }
            resourceListingToComponents(from.getResourceListing(), openAPI.getComponents());
        }
        if (openAPI.getComponents() == null) {
            openAPI.components(new Components());
        }
        documentationToComponents(from, openAPI.getComponents());
        openAPI.extensions(this.vendorExtensionsMapper.mapExtensions(from.getVendorExtensions()));
        openAPI.paths(mapPaths(from.getApiListings()));
        openAPI.externalDocs(mapExternalDocs(from.getExternalDocumentation()));
        openAPI.info(mapApiInfo(fromResourceListingInfo(from)));
        openAPI.servers(serverListToServerList(from.getServers()));
        openAPI.tags(tagSetToTagList(from.getTags()));
        openAPI.openapi("3.0.3");
        return openAPI;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    Operation mapOperation(springfox.documentation.service.Operation from, ModelNamesRegistry modelNamesRegistry) {
        if (from == null) {
            return null;
        }
        Operation operation = new Operation();
        operation.security(this.securityMapper.mapFrom(from.getSecurityReferences()));
        operation.extensions(this.vendorExtensionsMapper.mapExtensions(from.getVendorExtensions()));
        operation.requestBody(map(from.getBody(), modelNamesRegistry));
        operation.operationId(from.getUniqueId());
        operation.description(from.getNotes());
        operation.parameters(requestParameterSortedSetToParameterList(from.getQueryParameters(), modelNamesRegistry));
        Set<String> set = from.getTags();
        if (set != null) {
            operation.tags(new ArrayList(set));
        }
        operation.summary(from.getSummary());
        operation.responses(map((Set<Response>) from.getResponses(), modelNamesRegistry));
        if (from.getDeprecated() != null) {
            operation.deprecated(Boolean.valueOf(Boolean.parseBoolean(from.getDeprecated())));
        }
        return operation;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    Parameter mapParameter(RequestParameter from, ModelNamesRegistry modelNamesRegistry) {
        if (from == null) {
            return null;
        }
        Parameter parameter = new Parameter();
        parameter.schema(fromSimpleParameter(fromParameterSpecificationQuery(from), modelNamesRegistry));
        parameter.in(fromInIn(from));
        parameter.name(from.getName());
        parameter.description(from.getDescription());
        parameter.required(from.getRequired());
        parameter.deprecated(from.getDeprecated());
        parameter.examples(this.examplesMapper.mapExamples(from.getExamples()));
        parameter.extensions(this.vendorExtensionsMapper.mapExtensions(from.getExtensions()));
        parameter.allowReserved((Boolean) from.getParameterSpecification().getQuery().map(q -> {
            return q.getAllowReserved();
        }).orElse(null));
        parameter.explode((Boolean) from.getParameterSpecification().getQuery().map(q -> {
            return q.getExplode();
        }).orElse(null));
        parameter.allowEmptyValue((Boolean) from.getParameterSpecification().getQuery().map(q -> {
            return q.getAllowEmptyValue();
        }).orElse(null));
        parameter.style((Parameter.StyleEnum) from.getParameterSpecification().getQuery().map(q -> {
            return parameterStyle(q.getStyle());
        }).orElse(null));
        afterMappingParameter(from, parameter);
        return parameter;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    PathItem.HttpMethod mapHttpMethod(HttpMethod method) {
        PathItem.HttpMethod httpMethod;
        if (method == null) {
            return null;
        }
        switch (AnonymousClass1.$SwitchMap$org$springframework$http$HttpMethod[method.ordinal()]) {
            case 1:
                httpMethod = PathItem.HttpMethod.GET;
                break;
            case 2:
                httpMethod = PathItem.HttpMethod.HEAD;
                break;
            case 3:
                httpMethod = PathItem.HttpMethod.POST;
                break;
            case 4:
                httpMethod = PathItem.HttpMethod.PUT;
                break;
            case 5:
                httpMethod = PathItem.HttpMethod.PATCH;
                break;
            case 6:
                httpMethod = PathItem.HttpMethod.DELETE;
                break;
            case 7:
                httpMethod = PathItem.HttpMethod.OPTIONS;
                break;
            case 8:
                httpMethod = PathItem.HttpMethod.TRACE;
                break;
            default:
                throw new IllegalArgumentException("Unexpected enum constant: " + method);
        }
        return httpMethod;
    }

    /* renamed from: springfox.documentation.oas.mappers.ServiceModelToOpenApiMapperImpl$1  reason: invalid class name */
    /* loaded from: springfox-oas-3.0.0.jar:springfox/documentation/oas/mappers/ServiceModelToOpenApiMapperImpl$1.class */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$org$springframework$http$HttpMethod = new int[HttpMethod.values().length];

        static {
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.GET.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.HEAD.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.POST.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.PUT.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.PATCH.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.DELETE.ordinal()] = 6;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.OPTIONS.ordinal()] = 7;
            } catch (NoSuchFieldError e7) {
            }
            try {
                $SwitchMap$org$springframework$http$HttpMethod[HttpMethod.TRACE.ordinal()] = 8;
            } catch (NoSuchFieldError e8) {
            }
        }
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected MediaType fromRepresentation(Representation each, ModelNamesRegistry modelNamesRegistry) {
        if (each == null) {
            return null;
        }
        MediaType mediaType = new MediaType();
        mediaType.schema(this.schemaMapper.mapModel(each.getModel(), modelNamesRegistry));
        List<VendorExtension> facetExtensions = eachModelFacetExtensions(each);
        mediaType.extensions(this.vendorExtensionsMapper.mapExtensions(facetExtensions));
        mediaType.encoding(fromEncodings(each.getEncodings(), modelNamesRegistry));
        return mediaType;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected Encoding mapEncoding(springfox.documentation.service.Encoding from, ModelNamesRegistry modelNamesRegistry) {
        if (from == null) {
            return null;
        }
        Encoding encoding = new Encoding();
        encoding.setStyle(this.styleEnumMapper.encodingStyle(from.getStyle()));
        encoding.setContentType(from.getContentType());
        encoding.setHeaders(fromHeaders(from.getHeaders(), modelNamesRegistry));
        encoding.setExplode(from.getExplode());
        encoding.setAllowReserved(from.getAllowReserved());
        encoding.extensions(this.vendorExtensionsMapper.mapExtensions(from.getExtensions()));
        return encoding;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected Header mapHeader(springfox.documentation.service.Header from, ModelNamesRegistry modelNamesRegistry) {
        if (from == null) {
            return null;
        }
        Header header = new Header();
        header.schema(this.schemaMapper.mapFrom(from.getModelSpecification(), modelNamesRegistry));
        header.required(from.getRequired());
        header.description(from.getDescription());
        return header;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected Info mapApiInfo(ApiInfo from) {
        if (from == null) {
            return null;
        }
        Info info = new Info();
        info.license(this.licenseMapper.apiInfoToLicense(from));
        info.extensions(this.vendorExtensionsMapper.mapExtensions(from.getVendorExtensions()));
        info.termsOfService(from.getTermsOfServiceUrl());
        info.contact(map(from.getContact()));
        info.title(from.getTitle());
        info.description(from.getDescription());
        info.version(from.getVersion());
        return info;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected Contact map(springfox.documentation.service.Contact from) {
        if (from == null) {
            return null;
        }
        Contact contact = new Contact();
        contact.name(from.getName());
        contact.url(from.getUrl());
        contact.email(from.getEmail());
        return contact;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected Tag mapTag(springfox.documentation.service.Tag from) {
        if (from == null) {
            return null;
        }
        Tag tag = new Tag();
        tag.extensions(this.vendorExtensionsMapper.mapExtensions(from.getVendorExtensions()));
        tag.name(from.getName());
        tag.description(from.getDescription());
        return tag;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected Server mapServer(springfox.documentation.service.Server from) {
        if (from == null) {
            return null;
        }
        Server server = new Server();
        server.extensions(this.vendorExtensionsMapper.mapExtensions(from.getExtensions()));
        server.url(from.getUrl());
        server.description(from.getDescription());
        server.variables(serverVariableMap(from.getVariables()));
        return server;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected ServerVariable mapServerVariable(springfox.documentation.service.ServerVariable from) {
        if (from == null) {
            return null;
        }
        ServerVariable serverVariable = new ServerVariable();
        serverVariable.setDefault(from.getDefaultValue());
        List<String> list = from.getAllowedValues();
        if (list != null) {
            serverVariable.setEnum(new ArrayList(list));
        }
        serverVariable.description(from.getDescription());
        serverVariable.extensions(this.vendorExtensionsMapper.mapExtensions(from.getExtensions()));
        return serverVariable;
    }

    @Override // springfox.documentation.oas.mappers.ServiceModelToOpenApiMapper
    protected ExternalDocumentation mapExternalDocs(springfox.documentation.common.ExternalDocumentation from) {
        if (from == null) {
            return null;
        }
        ExternalDocumentation externalDocumentation = new ExternalDocumentation();
        externalDocumentation.description(from.getDescription());
        externalDocumentation.url(from.getUrl());
        externalDocumentation.extensions(this.vendorExtensionsMapper.mapExtensions(from.getExtensions()));
        return externalDocumentation;
    }

    protected void resourceListingToComponents(ResourceListing resourceListing, Components mappingTarget) {
        if (resourceListing != null) {
            if (mappingTarget.getSecuritySchemes() != null) {
                Map<String, SecurityScheme> map = this.securitySchemeMapper.map(resourceListing.getSecuritySchemes());
                if (map != null) {
                    mappingTarget.getSecuritySchemes().clear();
                    mappingTarget.getSecuritySchemes().putAll(map);
                    return;
                }
                mappingTarget.securitySchemes((Map) null);
                return;
            }
            Map<String, SecurityScheme> map2 = this.securitySchemeMapper.map(resourceListing.getSecuritySchemes());
            if (map2 != null) {
                mappingTarget.securitySchemes(map2);
            }
        }
    }

    protected void documentationToComponents(Documentation documentation, Components mappingTarget) {
        if (documentation != null) {
            if (mappingTarget.getSchemas() != null) {
                Map<String, Schema> map = this.schemaMapper.modelsFromApiListings(documentation.getApiListings());
                if (map != null) {
                    mappingTarget.getSchemas().clear();
                    mappingTarget.getSchemas().putAll(map);
                    return;
                }
                mappingTarget.schemas((Map) null);
                return;
            }
            Map<String, Schema> map2 = this.schemaMapper.modelsFromApiListings(documentation.getApiListings());
            if (map2 != null) {
                mappingTarget.schemas(map2);
            }
        }
    }

    private ApiInfo fromResourceListingInfo(Documentation documentation) {
        ResourceListing resourceListing;
        ApiInfo info;
        if (documentation == null || (resourceListing = documentation.getResourceListing()) == null || (info = resourceListing.getInfo()) == null) {
            return null;
        }
        return info;
    }

    protected List<Server> serverListToServerList(List<springfox.documentation.service.Server> list) {
        if (list == null) {
            return null;
        }
        List<Server> list1 = new ArrayList<>(list.size());
        for (springfox.documentation.service.Server server : list) {
            list1.add(mapServer(server));
        }
        return list1;
    }

    protected List<Tag> tagSetToTagList(Set<springfox.documentation.service.Tag> set) {
        if (set == null) {
            return null;
        }
        List<Tag> list = new ArrayList<>(set.size());
        for (springfox.documentation.service.Tag tag : set) {
            list.add(mapTag(tag));
        }
        return list;
    }

    protected List<Parameter> requestParameterSortedSetToParameterList(SortedSet<RequestParameter> sortedSet, ModelNamesRegistry modelNamesRegistry) {
        if (sortedSet == null) {
            return null;
        }
        List<Parameter> list = new ArrayList<>(sortedSet.size());
        for (RequestParameter requestParameter : sortedSet) {
            list.add(mapParameter(requestParameter, modelNamesRegistry));
        }
        return list;
    }

    private Optional<SimpleParameterSpecification> fromParameterSpecificationQuery(RequestParameter requestParameter) {
        ParameterSpecification parameterSpecification;
        Optional<SimpleParameterSpecification> query;
        if (requestParameter == null || (parameterSpecification = requestParameter.getParameterSpecification()) == null || (query = parameterSpecification.getQuery()) == null) {
            return null;
        }
        return query;
    }

    private String fromInIn(RequestParameter requestParameter) {
        ParameterType in;
        String in1;
        if (requestParameter == null || (in = requestParameter.getIn()) == null || (in1 = in.getIn()) == null) {
            return null;
        }
        return in1;
    }

    private List<VendorExtension> eachModelFacetExtensions(Representation representation) {
        ModelSpecification model;
        List<VendorExtension> facetExtensions;
        if (representation == null || (model = representation.getModel()) == null || (facetExtensions = model.getFacetExtensions()) == null) {
            return null;
        }
        return facetExtensions;
    }
    
}
